package com.eco.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.config.entity.EquMold;
import com.config.entity.Equipment;
import com.config.entity.LineConfig;
import com.config.service.EquMoldService;
import com.config.service.EquipmentService;
import com.config.service.LineConfigService;
import com.eco.service.ECOService;


@RequestMapping("/energy")
@Controller
public class EcoController {

	@Resource(name="ecoService")
	private ECOService ecoService;
	@Resource(name="equMoldService")
	private EquMoldService equMoldService;
	@Resource(name="equipmentService")
	private EquipmentService equipService;
	@Resource(name="lineConfigService")
	private LineConfigService lineConfigService;
	

	
	/**
	 * 节能---同类设备能力分析
	 * @param model
	 * @param type
	 * @return
	 */
	@RequestMapping("/capacityAnalysis/{type}")
	public String capacityAnalysis(Model model,@PathVariable(value = "type") String type){
		model.addAttribute("currentName","同类设备能力分析");
		if(type.equals("complex")){
			return "energy/capacityanalysis/capacitycomplex";
		}else if(type.equals("week")){
			return "energy/capacityanalysis/capacityweek";
		}else{
			return "energy/capacityanalysis/capacitymonth";
		}
	}
	@RequestMapping("/capasityData/{type}")
	public Map<String, Object> capasityData(@PathVariable(value = "type") String type,int equMoldId,
			@RequestParam(value="startDate",required=false,defaultValue="0")String startDate,
			@RequestParam(value="endDate",required=false,defaultValue="0")String endDate,
			@RequestParam(value="year",required=false,defaultValue="0")int year,
			@RequestParam(value="startTime",required=false,defaultValue="0")int startTime,
			@RequestParam(value="endTime",required=false,defaultValue="0")int endTime){
		Map<String, Object> model=new HashMap<String, Object>();
		if(equMoldId==0){
			List<EquMold> equList=equMoldService.findAll();
			if(equList.size()>0){
				equMoldId=equList.get(0).getId();
			}else{
				model.put("success", false);
				model.put("msg", "无设备类型,请先添加设备类型数据");
				return model;
			}
		}
		List<Equipment> equipList=equipService.findByEquModlId(equMoldId);
		if(equipList.size()>0){
			if(type.equals("complex")){
				ecoService.getEquModeAbilityDayData(equMoldId, startDate, endDate, model);
			}else if(type.equals("week")){
				ecoService.getEquModeAbilityWeekData(equMoldId, year, startTime, endTime, model);
			}else{
				ecoService.getEquModeAbilityMonthData(equMoldId, year, startTime, endTime, model);
			}
			model.put("success", true);
		}else{
			model.put("success", false);
			if(type.equals("complex")){
				model.put("startDate", startDate);
				model.put("endDate", endDate);
			}else if(type.equals("week")){
				model.put("startDate", year+"年第"+startTime+"周");
				model.put("endDate", year+"年第"+endTime+"周");
			}else{
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
				try {
					model.put("startDate", sdf.format(sdf.parse(year+"-"+startTime)));
					model.put("endDate", sdf.format(sdf.parse(year+"-"+endTime)));
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}
			EquMold equModel=equMoldService.getEquMold(equMoldId);
			model.put("equMold", equModel.getEquType());
			model.put("totalEqus", 0);
			model.put("averageFullRate", "0%");
			model.put("freeEqu", "无");
			model.put("msg", "无设备信息，请重新选择设备类型");
		}
		return model;
	}
	
	
	/**
	 * 节能---单机设备老化分析
	 * @param model
	 * @param type
	 * @return
	 */
	@RequestMapping("/agingAnalysis/{type}")
	public String agingAnalysis(Model model,@PathVariable(value = "type") String type){
		model.addAttribute("currentName","单机设备老化分析");
		if(type.equals("week")){
			return "energy/aginganalysis/agingweek";
		}else{
			return "energy/aginganalysis/agingmonth";
		}
	}
	
	
	@RequestMapping("/agingData/{type}")
	public Map<String, Object> agingData(@PathVariable(value = "type") String type,int equId,
			@RequestParam(value="year",required=false,defaultValue="0")int year,
			@RequestParam(value="start",required=false,defaultValue="0")int start,
			@RequestParam(value="end",required=false,defaultValue="0")int end){
		Map<String, Object> model=new HashMap<String, Object>();
		if(equId==0){
			//获取第一条生产线
			LineConfig line=lineConfigService.findFirstLine();
			Equipment equ=equipService.listByLine(line.getId()).get(0);
			equId=equ.getId();
		}
		if(type.equals("week")){
			ecoService.getEquAgingAnalysisWeekData(equId, year, start, end, model);
		}else if(type.equals("month")){
			ecoService.getEquAgingAnalysisMonthData(equId, year, start, end, model);
		}
		return model;
	}
}
